let dateFormat = function (timestamp, format) {
	// 通过getDate()方法获取date类型的时间
	var regYear = new RegExp("(y+)", "i");
	var realDate = new Date(parseInt(timestamp));
	console.log(timestamp);

	function timeFormat(num) {
		return num < 10 ? '0' + num : num;
	}

	var date = [
		["M+", timeFormat(realDate.getMonth() + 1)],
		["D+", timeFormat(realDate.getDate())],
		["h+", timeFormat(realDate.getHours())],
		["m+", timeFormat(realDate.getMinutes())],
		["s+", timeFormat(realDate.getSeconds())],
		["q+", Math.floor((realDate.getMonth() + 3) / 3)],
		["S+", realDate.getMilliseconds()],
	];
	var reg1 = regYear.exec(format);

	if (reg1) {
		format = format.replace(reg1[1], (realDate.getFullYear() + '').substring(4 - reg1[1].length));
	}
	for (var i = 0; i < date.length; i++) {
		var k = date[i][0];
		var v = date[i][1];
		// getRegExp初始化一个正则表达式对象
		var reg2 = new RegExp("(" + k + ")").exec(format);
		if (reg2) {
			format = format.replace(reg2[1], reg2[1].length == 1
				? v : ("00" + v).substring(("" + v).length));
		}
	}
	return format;
};

let date = {
	/**
	 * 时间戳格式化
	 * eg: date.format("YYYY年MM月DD")
	 * eg: date.format("yyyy年MM月dd")
	 * eg: date.format("yyyy年MM月DD hh:mm:ss")
	 * eg: date.format(1597228752352)
	 * eg: date.format(1597228752,"YY年MM月DD")
	 * eg: date.format(1597228752,"YYYY年MM月DD")
	 * eg: date.format(1597228752,"yy年MM月DD")
	 * eg: date.format(1597228752,"yyyy年MM月DD")
	 * @param timestamp 传入的时间戳(不传为当前时间，可传format格式,时间戳可以使秒或者毫秒)
	 * @param format    格式化（默认yyyy年MM年dd）
	 * @returns String
	 */
	format: function (timestamp, format) {
		format = format || 'yyyy年MM年DD';
		timestamp = timestamp || new Date() * 1;
		if (typeof timestamp == "string" && isNaN(timestamp * 1)) {
			let temp = timestamp.toLocaleLowerCase();
			if (temp.indexOf("y") >= 0 || temp.indexOf("m") || temp.indexOf("d") || temp.indexOf("h") || temp.indexOf("n")) {
				format = timestamp;
			}
			timestamp = new Date() * 1;
		}
		format = format.replace(/d/gi, 'D');
		timestamp = timestamp * 1;
		if (timestamp * 1 > 0 && timestamp.toString().length == 10) {
			timestamp = timestamp * 1000;
		}
		return dateFormat(timestamp, format);
	},
	time: function () { //返回当前以秒为单位的时间戳
		return parseInt(new Date() / 1000);
	}
};
export default date;